package com.djl.sqtg.controller;

import com.alibaba.fastjson.JSONObject;
import com.djl.sqtg.entity.GoodsSales;
import com.djl.sqtg.entity.LayuiResultData;
import com.djl.sqtg.entity.Orders;
import com.djl.sqtg.service.IColonelService;
import com.djl.sqtg.service.IOrderService;
import com.djl.sqtg.util.JsonResult;
import com.djl.sqtg.util.OrderNumberUtil;
import com.djl.sqtg.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("order")
public class OrderController extends BaseController{
    @Autowired
    private IOrderService orderService;
    @Autowired
    private IColonelService colonelService;
    /*
    * 添加订单
    * 商品数量与价格由前端传来
    * */
    @PostMapping({"addOrder"})
    public JsonResult addOrder(@RequestBody Orders orders){
        orders.setDealtime(DateUtil.getNowTime());
        orders.setOrderstate(0);
        orders.setOrdernumber(OrderNumberUtil.getOrderNum());
        System.out.println(orders);
        orderService.addOrder(orders);
        Orders data = orderService.findOrdersByOrdernumber(orders.getOrdernumber());
        return new JsonResult(OK,data);
    }

    /*
    * 确认付款-修改订单orderstate为1
    * */
    @GetMapping({"confirmPayment/{oid}"})
    public JsonResult confirmPayment(@PathVariable Integer oid){
        System.out.println(oid);
        Integer orderstate=1;
        orderService.updateOrderState(oid,orderstate);
        Orders data = orderService.findOrdersByOid(oid);
        return new JsonResult(OK,data);
    }

    /*
     * 取消订单-数据库订单表中数据状态为5-已取消
     * */
    @GetMapping({"cancelOrder/{oid}"})
    public JsonResult cancelOrder(@PathVariable Integer oid){
        System.out.println(oid);
        orderService.updateOrderState(oid,5);
        return new JsonResult(OK);
    }

    /*
     * 确认收获-修改订单orderstate为2
     * */
    @GetMapping({"confirmHarvest/{oid}"})
    public JsonResult confirmHarvest(@PathVariable Integer oid){
        Integer orderstate=2;
        orderService.updateOrderState(oid,orderstate);
        Orders data = orderService.findOrdersByOid(oid);
        System.out.println(data.toString());
        //用户确认收货，团长收取收益：团长提成率*该订单金额
        Integer royal=colonelService.getRoyalty(data.getTzid());
        System.out.println(royal);
        Float moneyItem=data.getDealprice()*royal/100;
        System.out.println(moneyItem);
        Float money=colonelService.getColonelMoney(data.getTzid());
        System.out.println(money);
        colonelService.updateColonelMoney(data.getTzid(), moneyItem+money);
        return new JsonResult(OK);
    }

    @GetMapping({"goodsTodaySalesByOrders"})
    public JsonResult<List<GoodsSales>> goodsTodaySalesByOrders() {
        SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();      // 获取当前时间
        String todayDate = sdfTime.format(date);
        System.out.println("今日日期：" + todayDate);
        List<GoodsSales> goodsSalesList = orderService.goodsSalesByOrders(todayDate);
        return new JsonResult<List<GoodsSales>>(OK, goodsSalesList);
    }
    @GetMapping({"getSevenOrder"})
    public JsonResult getSevenOrder() {
//        ArrayList dateList= DateUtil.pastDay(DateUtil.getToday());
        ArrayList dateList= DateUtil.pastDay("2023-05-17");

        ArrayList orderNumList=new ArrayList<>();
        JSONObject data =new JSONObject();
        for (int i=0;i<7;i++){
            System.out.println(dateList.get(i));
            Integer orderNum = orderService.getSevenOrder(String.valueOf(dateList.get(i)));
            System.out.println(orderNum);
            orderNumList.add(orderNum);
        }
        data.put("dateList",dateList);
        data.put("orderNumList",orderNumList);
        return new JsonResult(OK, data);
    }

//    获取某日的销售额
//    获取月销售额
    @GetMapping({"getDayTurnover/{date}"})
    public JsonResult getDayTurnover(@PathVariable String date) {
        float turnover = orderService.getTodayTurnover(date);
        return new JsonResult(OK, turnover);
    }
    //    获取七日的销售额
    @GetMapping({"getSevenTurnover"})
    public JsonResult getDayTurnover() {
        SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();      // 获取当前时间
        String todayDate = sdfTime.format(date);
        ArrayList<String> pastDaysList = DateUtil.pastDay(todayDate);
        System.out.println("七天日期："+pastDaysList);
        float turnover1=0;
        for (int i=0;i<pastDaysList.size();i++){
            if (orderService.getTodayTurnover(pastDaysList.get(i))!=null){
                turnover1 = turnover1+orderService.getTodayTurnover(pastDaysList.get(i));
            }
            System.out.println(i+"dayTurnover:"+orderService.getTodayTurnover(pastDaysList.get(i)));
            System.out.println("AllTurnover"+turnover1);
        }
        BigDecimal bigDecimal = new BigDecimal(turnover1);
        double turnover = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        System.out.println(turnover);
        return new JsonResult(OK, turnover);
    }
//获取昨日销售量

    @GetMapping({"goodsYesterdaySalesByOrders"})
    public JsonResult<List<GoodsSales>> goodsYesterdaySalesByOrders() {
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        int day = cal.get(Calendar.DAY_OF_MONTH)-1;
        String yesterday;
        if(day<10){
             yesterday=year+"-0"+month+"-0"+day;
        }else  {
            yesterday=year+"-0"+month+"-"+day;
        }
        System.out.println("昨日日期："+yesterday);
        List<GoodsSales> goodsSalesList = orderService.goodsSalesByOrders(yesterday);
        Float turnover = orderService.getTodayTurnover(yesterday);
        return new JsonResult<List<GoodsSales>>(OK, goodsSalesList);
    }

//获取七日销售量
    @GetMapping({"goodsSevenDaySalesByOrders"})
    public JsonResult<List<GoodsSales>> goodsSevenDaySalesByOrders() {
        SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();      // 获取当前时间
        String todayDate = sdfTime.format(date);
        ArrayList<String> pastDaysList = DateUtil.pastDay(todayDate);
        System.out.println("七天日期：");
        System.out.println(pastDaysList.toString());
        List<GoodsSales> goodsSevenDaySales=orderService.goodsSevenDaySalesByOrders(pastDaysList);
        System.out.println(goodsSevenDaySales.toString());
        return new JsonResult<List<GoodsSales>>(OK, goodsSevenDaySales);
    }
//获取月销售量
    @GetMapping({"goodsMonthSalesByOrders"})
    public JsonResult<List<GoodsSales>> goodsMonthSalesByOrders() {
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        String month1=month<10? "0"+month : String.valueOf(month);
        String monthstr=year+"-"+month1;
        System.out.println("今月：" + monthstr);
        List<GoodsSales> goodsMonthSales=orderService.goodsMonthSalesByOrders(monthstr);
        System.out.println(goodsMonthSales.toString());
        return new JsonResult<List<GoodsSales>>(OK, goodsMonthSales);
    }
/*
    @GetMapping({"getMonthTurnover"})
    public JsonResult getMonthTurnover() {
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        String month1=month<10? "0"+month : String.valueOf(month);
        String monthstr=year+"-"+month1;
        System.out.println("今月：" + monthstr);
        float turnover = orderService.getTodayTurnover(monthstr);
        return new JsonResult(OK, turnover);
    }*/

//    获取类别的七天销售量
    @GetMapping({"getCategoryWeekSales"})
    public JsonResult getCategoryWeekSales() {
        List data=new ArrayList<>();
        SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();      // 获取当前时间
        String todayDate = sdfTime.format(date);
//        ArrayList<String> pastDaysList = DateUtil.pastDay(todayDate);
        ArrayList<String> pastDaysList = DateUtil.pastDay("2023-05-17");

        System.out.println("七天日期：");
        System.out.println(pastDaysList.toString());
        ArrayList salesData = new ArrayList<>();
        ArrayList<String> categoryData = new ArrayList<>();

        for (int j=1;j<=10;j++){//循环每个类别
            String categoryname=orderService.getCategoryNameByGcid(j);
            categoryData.add(categoryname);
            ArrayList salesDataItem=new ArrayList<>();
            for (int i=0;i<pastDaysList.size();i++){//循环每一天
                Integer sales = orderService.getCategoryWeekSales(j, pastDaysList.get(i));
                salesDataItem.add(sales);
                System.out.println(categoryname+","+pastDaysList.get(i)+","+sales);
            }
            salesData.add(salesDataItem);
        }
        data.add(categoryData);
        data.add(pastDaysList);
        data.add(salesData);
        return new JsonResult(OK,data);
    }
//获取今日销售量销售额
    @GetMapping({"getTodaySalesTurnover"})
    public JsonResult getTodaySalesTurnover() {
        SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();      // 获取当前时间
        String todayDate = sdfTime.format(date);
        System.out.println("今日日期：" + todayDate);
        Float turnover = orderService.getTodayTurnover(todayDate);
        Integer sales = orderService.getTodaySales(todayDate);
        HashMap data=new HashMap<>();
        data.put("sales",sales);
        data.put("turnover",turnover);
        return new JsonResult(OK, data);
    }

    @GetMapping({"getGoosSalesTurnover"})
    public JsonResult<HashMap> getGoosSalesTurnover() {
        SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();      // 获取当前时间
        String todayDate = sdfTime.format(date);
        System.out.println("今日日期：" + todayDate);
        List<String> priceList = orderService.getGoosSales(todayDate);
        Integer sales=priceList.size();
        Float turnover= Float.valueOf(0);
        for (int i=0;i<priceList.size();i++){
            turnover= turnover+Float.valueOf( priceList.get(i));
        }
        System.out.println(priceList);
        System.out.println(turnover);
        HashMap data=new HashMap<>();
        data.put("sales",sales);
        data.put("turnover",turnover);
        return new JsonResult<HashMap>(OK, data);
    }

    @GetMapping({"getAllOrders"})
    public LayuiResultData getAllOrders(
                                        @RequestParam(defaultValue = "1") Integer pageNum,
                                        @RequestParam(defaultValue = "10") Integer pageSize,
                                        String dateRange,
                                        String username,
                                        String colonelname,
                                        String telep,
                                        String ordernumber,
                                        String orderstate) {
        System.out.println("pagenum："+pageNum);
        System.out.println("pageSize："+pageSize);
        System.out.println("dateRange："+dateRange);
        System.out.println("username："+username);
        System.out.println("colonelname："+colonelname);
        System.out.println("telep："+telep);
        System.out.println("ordernumber："+ordernumber);
        System.out.println("orderstate："+orderstate);
        List<Orders> data;
        Integer count;
        if ((dateRange==null && username==null && colonelname==null && telep==null && ordernumber==null&& orderstate==null)|| (dateRange=="" && username=="" && colonelname=="" && telep=="" && ordernumber=="" && orderstate=="")){
            data= orderService.getAllOrders((pageNum-1)*10,pageSize);
            count = orderService.getAllOrdersNum();
        }
        else {
            System.out.println("search..........");
            String startDate=null;
            String endDate=null;
            Integer orderstate0;
            if (orderstate==""){
                orderstate0=null;
            }else {
                orderstate0=Integer.valueOf(orderstate);
            }
            if (dateRange.isEmpty()){
                System.out.println("dateRange为空");
                data = orderService.getOrdersBySearch(startDate,endDate,username,colonelname,telep,ordernumber,orderstate0,(pageNum-1)*10,pageSize);
                count = orderService.getOrdersNumBySearch(startDate,endDate,username,colonelname,telep,ordernumber, orderstate0);
            }else {
                String[]  dateArry  = dateRange.split(" ~ ");
                data = orderService.getOrdersBySearch(dateArry[0],dateArry[1],username,colonelname,telep,ordernumber, orderstate0,(pageNum-1)*10,pageSize);
                count = orderService.getOrdersNumBySearch(dateArry[0],dateArry[1],username,colonelname,telep,ordernumber,orderstate0);
            }
        }
        System.out.println(data);
        LayuiResultData layuiResultData=new LayuiResultData();
        layuiResultData.setData(data);
        layuiResultData.setCount(count);
        layuiResultData.setPageNum(pageNum);
        layuiResultData.setPageSize(pageSize);
        return  layuiResultData;
    }







/*    @GetMapping({"goodsSalesByOrders"})
    public JsonResult<List<GoodsSales>> goodsSalesByOrders(){
        SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();      // 获取当前时间
        String todayDate = sdfTime.format(date);
        System.out.println("今日日期："+todayDate);
        List<GoodsSales> goodsSalesList = orderService.goodsSalesByOrders(todayDate);
        Integer[] gids = new Integer[goodsSalesList.size()];
        for (int i=0;i<goodsSalesList.size();i++){
            gids[i]=goodsSalesList.get(i).getGid();
        }
        List<GoodsSales>   gidgoodstitleList = orderService.getGoodsBygids(gids);
        for (int i=0;i<goodsSalesList.size();i++){
            for (int j=0;j<goodsSalesList.size();j++){
                if (goodsSalesList.get(i).getGid()==gidgoodstitleList.get(j).getGid()){
                    goodsSalesList.get(i).setGoodstitle(gidgoodstitleList.get(j).getGoodstitle());
                    break;
                }
            }
        }
        return new JsonResult<List<GoodsSales>>(OK,goodsSalesList);
    }*/

   /* @GetMapping({"goodsSalesByOrders"})
    public JsonResult<List<GoodsSales>> goodsSalesByOrders(){
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        int day = cal.get(Calendar.DAY_OF_MONTH)-1;
        String yesterday=year+"-0"+month+"-"+day;
        System.out.println("昨日日期："+yesterday);
        List<GoodsSales> goodsSalesList = orderService.goodsSalesByOrders(yesterday);
        System.out.println(goodsSalesList);
        Integer[] gids = new Integer[goodsSalesList.size()];
        for (int i=0;i<goodsSalesList.size();i++){
            gids[i]=goodsSalesList.get(i).getGid();
        }
        List<GoodsSales>   gidgoodstitleList = orderService.getGoodsBygids(gids);
        for (int i=0;i<goodsSalesList.size();i++){
            for (int j=0;j<goodsSalesList.size();j++){
                if (goodsSalesList.get(i).getGid()==gidgoodstitleList.get(j).getGid()){
                    goodsSalesList.get(i).setGoodstitle(gidgoodstitleList.get(j).getGoodstitle());
                    break;
                }
            }
        }
        return new JsonResult<List<GoodsSales>>(OK,goodsSalesList);
    }*/

/*    @GetMapping({"getSevenDayGoodsSales"})
    public JsonResult<List<GoodsSales>> getSevenDayGoodsSales(){
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        int day = cal.get(Calendar.DAY_OF_MONTH)-1;
        String yesterday=year+"-0"+month+"-"+day;
        System.out.println("昨日日期："+yesterday);
        List<GoodsSales> goodsSalesList = orderService.findGoodsDaySalesByOrders(yesterday);
        System.out.println(goodsSalesList);
        Integer[] gids = new Integer[goodsSalesList.size()];
        for (int i=0;i<goodsSalesList.size();i++){
            gids[i]=goodsSalesList.get(i).getGid();
        }
        List<GoodsSales>   gidgoodstitleList = orderService.getGoodsBygids(gids);
        for (int i=0;i<goodsSalesList.size();i++){
            for (int j=0;j<goodsSalesList.size();j++){
                if (goodsSalesList.get(i).getGid()==gidgoodstitleList.get(j).getGid()){
                    goodsSalesList.get(i).setGoodstitle(gidgoodstitleList.get(j).getGoodstitle());
                    break;
                }
            }
        }
        return new JsonResult<List<GoodsSales>>(OK,goodsSalesList);
    }*/

    //获取用户待付款的订单
    @GetMapping({"getUserNoPaymentOrder/{uid}"})
    public JsonResult getUserNoPayOrder(@PathVariable Integer uid) {
        System.out.println("-------");
        List<Orders> data = orderService.getUserOrderByState(uid,0);
        System.out.println(data);
        return new JsonResult<>(OK,data);
    }
    //获取用户待收获的订单
    @GetMapping({"getUserNoAcceptOrder/{uid}"})
    public JsonResult getUserNoAccOrder(@PathVariable Integer uid) {
        List<Orders> data = orderService.getUserOrderByState(uid,1);
        return new JsonResult<>(OK,data);
    }
    //获取用户待评价的订单
    @GetMapping({"getUserNoReviewOrder/{uid}"})
    public JsonResult getUserNoRevOrder(@PathVariable Integer uid) {
        List<Orders> data = orderService.getUserOrderByState(uid,2);
        return new JsonResult<>(OK,data);
    }

    //获取用户所选择团长的待收获的所有订单 state-1
    @GetMapping({"getUserNoAcceptOrderByUidCid/{uid}/{cid}"})
    public JsonResult getUserNoAcceptOrderByUidCid(@PathVariable Integer uid,@PathVariable Integer cid) {
        System.out.println("进入---------");
        System.out.println(uid);
        System.out.println(cid);
        List<Orders> data = orderService.getUserNoAcceptOrderByUidCid(uid,cid);
        if (data!=null){
            return new JsonResult<>(OK,data);
        }
       return new JsonResult<>(NO);
    }
    //获取用户所选择团长的待收获的所有订单根据订单号 state-1
    @GetMapping({"acceptOrderByOrdernumber/{ordernumber}"})
    public JsonResult acceptOrderByOrdernumber(@PathVariable String ordernumber) {
        System.out.println("进入---------");
        System.out.println(ordernumber);
        Integer data = orderService.updateOrderStateByNum(ordernumber,2);
        System.out.println("次数"+data);
        if (data==1){
            return new JsonResult<>(OK);
        }
        return new JsonResult<>(NO);
    }

//    @GetMapping({"getUserAllOrder/{uid}"})
//    public JsonResult getUserAllOrder(@PathVariable Integer uid) {
//        List<Orders> data = orderService.getUserAllOrder(uid,2);
//        return new JsonResult<>(OK,data);
//    }



}
